﻿// ***********************************************************************
// Assembly         : ELS
// Author           : linhvn
// Created          : 10-20-2012
//
// Last Modified By : linhvn
// Last Modified On : 10-22-2012
// ***********************************************************************
// <copyright file="LogInForm.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace ELS.User
{
    /// <summary>
    /// Class LogInForm
    /// </summary>
    public partial class LogInForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LogInForm" /> class.
        /// </summary>
        public LogInForm()
        {
            //LogFile.CreateLogFiles();
            
            // initialize form
            InitializeComponent();

            // get user information from current system's registry
            GetUserInfoFromRegistry();
        }

        /// <summary>
        /// Gets the user info from registry.
        /// </summary>
        private void GetUserInfoFromRegistry()
        {
            string[] userInfo = userController.GetRememberUser();
            txtUsername.Text = userInfo[0];
            txtPassword.Text = userInfo[1];

            isRememberPassword = userInfo[0].Length > 0;
            if (isRememberPassword)
                ckbRememberPassword.Checked = true;
            else
                ckbRememberPassword.Checked = false;
        }

        /// <summary>
        /// The user controller
        /// </summary>
        UserController userController = new UserController();

        /// <summary>
        /// The current user's username
        /// </summary>
        public static string username = "";

        /// <summary>
        /// The password
        /// </summary>
        static string password = "";

        /// <summary>
        /// The is log in success
        /// </summary>
        public static bool isLogInSuccess = false;

        /// <summary>
        /// The is remember password
        /// </summary>
        bool isRememberPassword = false;

        /// <summary>
        /// Execute Login.
        /// </summary>
        public void LogIn()
        {            
            if (txtUsername.Text.Trim().Length > 0)
            {
                if (txtPassword.Text.Trim().Length > 0)
                {
                    username = txtUsername.Text.Trim();
                    password = txtPassword.Text.Trim();
                    int result = userController.LogIn(username, password);
                    switch (result)
                    {
                        case -1:
                            lblStatus.Text = UserCommon.LogInFail;
                            lblStatus.ForeColor = Color.Red;
                            break;
                        case 0:                            
                            lblStatus.Text = UserCommon.LogInNotExitUser;
                            lblStatus.ForeColor = Color.Red;
                            break;
                        case 1:
                            lblStatus.Text = UserCommon.LogInIncorrectPassword;
                            lblStatus.ForeColor = Color.Red;
                            break;
                        case 2:
                            lblStatus.Text = UserCommon.LogInSuccess;
                            lblStatus.ForeColor = Color.Green;
                       
                            isLogInSuccess = true;

                            this.Controls.Clear();
                            this.Hide();
                            new MainForm(username).Show();
                            UserCommon.loggedInUserID = int.Parse(userController.GetUserInfoByUsername(username).UserId);

                            if (isLogInSuccess)
                            {
                                // saving user' ID/PW to system's registry
                                isRememberPassword = ckbRememberPassword.Checked;
                                if (isRememberPassword)
                                {
                                    userController.SaveToRegistry(username, password);
                                }
                                else
                                    userController.SaveToRegistry(String.Empty, String.Empty);
                            }                            
                            break;
                    }
                }
                else
                {
                    // notify when user don't enter password
                    lblStatus.Text = UserCommon.LogInDoNotEnterPassword;            
                    lblStatus.ForeColor = Color.Red;
                }
            }
            else
            {
                // notify when user don't enter username
                lblStatus.Text = UserCommon.LogInDoNotEnterUsername;
                lblStatus.ForeColor = Color.Red;
            }
        }

        /// <summary>
        /// Threads to process show MainForm form.
        /// </summary>
        public static void ThreadProc()
        {
            Application.Run(new MainForm(username));
        }

        /// <summary>
        /// Handles the KeyPress Enter event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyPressEventArgs" /> instance containing the event data.</param>
        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LogIn();
            } 
        }

        /// <summary>
        /// Handles the KeyPress Enter event of the txtUsername control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyPressEventArgs" /> instance containing the event data.</param>
        private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LogIn();
            } 
        }

        /// <summary>
        /// Handles the KeyPress event Enter of the ckbRememberPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyPressEventArgs" /> instance containing the event data.</param>
        private void ckbRememberPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LogIn();
            } 
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            LogIn();
        }

        private void close_app(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }      
    }
}
